package com.ruoyi.partybuilding.controller;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.partybuilding.common.EQueryController;
import com.ruoyi.partybuilding.service.impl.OrgServiceImpl;
import com.ruoyi.partybuilding.service.impl.TeamRecordServiceImpl;
import lombok.var;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.domain.TeamRecord;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.*;
/**
 * 班子记录Controller
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Api(tags = "班子记录")
@RestController
@RequestMapping("/partybuilding/TeamRecord")
public class TeamRecordController extends EQueryController<TeamRecord>
{
    @Resource
    TeamRecordServiceImpl  teamRecordService;

    @Resource
    OrgServiceImpl orgService;

    @ApiOperation(value = "获取部门当前有效团队",notes = "届数最大且在任期内")
    @PostMapping("/nowEffectiveTeam")
    R<List<TeamRecord>> nowEffectiveTeam(Long deptId){
        return teamRecordService.nowEffectiveTeam(deptId);
    }
    /**
     * 导出班子记录列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:export')")
    @Log(title = "班子记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export( TeamRecord teamRecord, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(teamRecord,pageRequest,null,response);
    }

    /**
     * 获取班子记录详细信息
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:query')")
    @GetMapping(value = "/{id}")
    public R<TeamRecord> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }

    /**
     * 查询班子记录列表
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:list')")
    @GetMapping("/query")
    public R<Page<TeamRecord>> query(TeamRecord whereObj, PageDomain pageRequest) {
        return super.queryPage(whereObj, pageRequest);
    }
    /**
     * 查询班子记录列表
     */
    @ApiOperation("换届信息查询，包含成员")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:list')")
    @GetMapping("/queryChilds")
    public R<List<TeamRecord>> queryChilds(TeamRecord entity) {
        var list = equeryable().include(p -> p.many(TeamRecord::getTeamPersons))
                .whereObject(entity).orderByDesc(p -> p.column(TeamRecord::getYear)).toList();
        var maps=orgService.deptSetToMap(list,null);
        return (R)R.ok(maps);
    }
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeamRecord entity, PageDomain pageRequest) {
        var page= super.queryPage(entity, pageRequest);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getData().getContent());
        rspData.setTotal(page.getData().getTotalElements());
        return rspData;
    }


    /**
     * 新增班子记录
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:add')")
    @Log(title = "班子记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@Validated @RequestBody TeamRecord teamRecord)
    {
        var old=queryHelper.query().queryable(TeamRecord.class)
                .where(p->p.eq(TeamRecord::getYear,teamRecord.getYear()).eq(TeamRecord::getDeptId,teamRecord.getDeptId()))
                .firstOrNull();
        if(old!=null)
            return  R.fail("班子信息重复");
        return super.add(teamRecord);
    }

    /**
     * 修改班子记录
     */
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:edit')")
    @Log(title = "班子记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R edit(@Validated @RequestBody TeamRecord teamRecord) {
        var old = equeryable().where(p -> p.eq(TeamRecord::getId, teamRecord.getId())).firstOrNull();
        teamRecord.setYear(old.getYear());
        teamRecord.setDeptId(old.getDeptId());
        return super.edit(teamRecord);
    }

    /**
     * 删除班子记录
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:TeamRecord:remove')")
    @Log(title = "班子记录", businessType = BusinessType.DELETE)
	@PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable Long[] ids)
    {
       return super.remove(Arrays.stream(ids).toArray());
    }
}
